package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.logging.Level;

import javax.swing.JLabel;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.gui.components.ActionButton;
import be.kuleuven.cs.mop.gui.components.DateInput;
import be.kuleuven.cs.mop.gui.util.SwingUtils;



/**
 * {@link Dialog} for the "Adjust Clock Value" use case
 */
public class AdjustClockValueDialog extends ActionDialog {
	private static final long serialVersionUID = 1L;
	
	
	private DateInput input = new DateInput();
	
	
	public AdjustClockValueDialog(final Frame parent, final Controllers controllers) {
		super(parent, "Adjust Clock Value", controllers);
		final GridBagConstraints gbc = SwingUtils.getGBC();
		
		final JLabel label;
		try {
			label = new JLabel(
					getControllers().getControllerUsers().getTime().getTime().toString(),
					JLabel.CENTER);
		} catch (final Exception e) {
			log(e);
			return;
		}
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		add(label, gbc);
		
		input.setToolTipText("Format:  yyyy-mm-ddThh:mm:ss");
		gbc.gridy	= 1;
		add(input, gbc);
		
		gbc.gridy	= 2;
		add(new ActionButton("Adjust", new Dimension(150, 32), this, "adjust"), gbc);
		
		display();
	}
	
	
	
	@Override
	protected void handle(final ActionEvent event) {
		final Calendar date = input.read();
		try {
			getControllers().getControllerUsers().adjustClockValue(date);
			log("System time changed to '%s'", date.getTime());
		} catch (final InvalidCurrentUserException e) {
			log("The current user does not have sufficient access rights for this use case", Level.WARNING);
		}
		dispose();
	}
	
}